package stream;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : ZhuQi
 * @version : 时间:2021/12/28 16:43 版本:
 * @description :
 */
public class StreamSort {


    public static void main(String[] args) {
        ArrayList<Person> personList = new ArrayList<>();

        personList.add(new Person("tom", 20, 5000));
        personList.add(new Person("lili", 18, 3000));
        personList.add(new Person("jack", 25, 3000));

//        // 工资自然升序排序
//        List<Person> collect = personList.stream().sorted(Comparator.comparing(Person::getSalary)).collect(Collectors.toList());
//        collect.forEach(System.out::println);
//
//        // 工资降序排序
//        List<Person> collect1 = personList.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());
//        collect1.forEach(System.out::println);

        // 先按工资排序 再按年龄排序
        List<Person> collect2 = personList.stream().sorted((p1, p2) -> {
            if (p1.getSalary().compareTo(p2.getSalary()) == 0) {
                return p1.getAge().compareTo(p2.getAge());
            } else {
                return p1.getSalary().compareTo(p2.getSalary());
            }
        }).collect(Collectors.toList());
        collect2.forEach(System.out::println);
    }

}

@Data
class Person{
    private String name;
    private Integer age;
    private Integer salary;

    public Person(String name, Integer age, Integer salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
}